<?php
/*
File Name:		config.php
Description: 	Definisce i custom fields della tassonomia view
Plugin URI:  	http://www.SyriusWeb.com/
Author:      	SyriusWeb
Author URI:  	http://www.SyriusWeb.com/
*/


$sql_viewmeta = "CREATE TABLE IF NOT EXISTS `$table_viewmeta` (
	`meta_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
	`view_id` BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
	`meta_key` VARCHAR(255) NULL DEFAULT NULL COLLATE 'latin1_general_ci',
	`meta_value` LONGTEXT NULL COLLATE 'latin1_general_ci',
	PRIMARY KEY (`meta_id`),
	INDEX `view_id` (`view_id`),
	INDEX `meta_key` (`meta_key`)
)
COLLATE='latin2_general_ci'
ENGINE=InnoDB;";




add_action('init', 'fascicoli_viewcustomfields_init' ); // aggiungo i custom fields a view 
//le action necessarie alla creazione dei custom field per la tassonomia view
function fascicoli_viewcustomfields_init() {
    
	global $wpdb;
	
	add_action( 'view_edit_form_fields', 'fascicoli_edit_view_echo', 10, 1);	
	add_action( 'view_add_form_fields','fascicoli_edit_view_echo', 10, 1);	
	add_action( 'edited_view', 'fascicoli_save_view', 10, 1);	
	add_action( 'created_view', 'fascicoli_save_view', 10, 1);
	
	// segnalo a wordpress l'esistenza della nuova tabella per i custom meta di view
	$wpdb->viewmeta = $wpdb->prefix."viewmeta";
}



function fascicoli_edit_view_echo($tag) {
	fascicoli_edit_view($tag, false);
}



function fascicoli_edit_view_return($tag) {
	fascicoli_edit_view($tag, true);
}



// creo i custom field per la tassonomia view
function fascicoli_edit_view($tag, $return) {
    $group_by_field_view = get_metadata($tag->taxonomy, $tag->term_id, 'group_by_field_view', true);
	show_field('group_by_field_view', 'text', 'Group By Field', $group_by_field_view, 'FASCICOLI_DOMAIN','Group By Field', $return);

	$order_by_field_view = get_metadata($tag->taxonomy, $tag->term_id, 'order_by_field_view', true);
	show_field('order_by_field_view', 'text', 'Order By Field', $order_by_field_view, 'FASCICOLI_DOMAIN','Order By Field', $return);
	
	$order_asc_view = get_metadata($tag->taxonomy, $tag->term_id, 'order_asc_view', true);
	show_field('order_asc_view', 'checkbox', 'Order Asc', $order_asc_view, 'FASCICOLI_DOMAIN','Order Asc', $return);
	
	$view_first_only_view = get_metadata($tag->taxonomy, $tag->term_id, 'view_first_only_view', true);
	show_field('view_first_only_view', 'checkbox', 'View First Only', $view_first_only_view, 'FASCICOLI_DOMAIN','View First Only', $return);
	
	$expand_group_view = get_metadata($tag->taxonomy, $tag->term_id, 'expand_group_view', true);
	show_field('expand_group_view', 'checkbox', 'Expand Group', $expand_group_view, 'FASCICOLI_DOMAIN','Expand Group', $return);
	
	$footer_message_view = get_metadata($tag->taxonomy, $tag->term_id, 'footer_message_view', true);
	show_field('footer_message_view', 'text', 'Footer message', $footer_message_view, 'FASCICOLI_DOMAIN','Footer message', $return);
	
	$header_message_view = get_metadata($tag->taxonomy, $tag->term_id, 'header_message_view', true);
	show_field('header_message_view', 'text', 'Header message', $header_message_view, 'FASCICOLI_DOMAIN','Header message', $return);	
}



// salvataggio dei custom field per la tassonomia view
function fascicoli_save_view($term_id) {
    if (!$term_id) return;
    
    if (isset($_POST['group_by_field_view'])) {
	    update_metadata('view', $term_id, 'group_by_field_view', $_POST['group_by_field_view']);
	}
	
	if (isset($_POST['order_by_field_view'])) {
	    update_metadata('view', $term_id, 'order_by_field_view', $_POST['order_by_field_view']);
	}
	
	if (isset($_POST['order_asc_view'])) {
	    update_metadata('view', $term_id, 'order_asc_view', $_POST['order_asc_view']);
	}
	
	if (isset($_POST['view_first_only_view'])) {
	    update_metadata('view', $term_id, 'view_first_only_view', $_POST['view_first_only_view']);
	}
	
	if (isset($_POST['expand_group_view'])) {
	    update_metadata('view', $term_id, 'expand_group_view', $_POST['expand_group_view']);
	}
	
	if (isset($_POST['footer_message_view'])) {
	    update_metadata('view', $term_id, 'footer_message_view', $_POST['footer_message_view']);
	}
	
	if (isset($_POST['header_message_view'])) {
	    update_metadata('view', $term_id, 'header_message_view', $_POST['header_message_view']);
	}	
}


add_action('activate_plugin', 'fascicoli_activate_view');
//creo la tabella per il campo aggiuntivo della tassonomia "Categorie"	
function fascicoli_activate_view() {
	global $wpdb;
 	global $sql_viewmeta;	
	 
	dbDelta($sql_viewmeta);	
}
